package com.awenhui.demo.controller;

import com.awenhui.demo.core.base.ActionResult;
import com.awenhui.demo.core.base.BaseController;
import com.awenhui.demo.core.utils.SnowFlake;
import com.awenhui.demo.dao.*;
import com.awenhui.demo.model.entity.RoleEntity;
import com.awenhui.demo.model.entity.UserEntity;
import com.awenhui.demo.model.entity.UserRoleEntity;
import com.awenhui.demo.model.rbac.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 权限管理
 *
 * @author yuxf
 * @version 1.0
 * @date 2020/5/6 9:31
 */
@Api(value = "权限管理", description = "权限管理")
@RequestMapping("/rbac")
@RestController
public class RbacController extends BaseController {

    @Autowired
    UserAnnotationMapper userAnnotationMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    /**
     * 查询用户
     *
     * @return
     */
    @ApiOperation("mapper配置文件sql查询用户")
    @GetMapping("configGetUsers")
    public ActionResult<?> configGetUsers(SearchUserModel model) {
        List<UserEntity> userEntities = userMapper.configGetUsers(model);
        return success(userEntities);
    }

    /**
     * 查询用户
     *
     * @return
     */
    @ApiOperation("mapper配置文件sql查询用户")
    @GetMapping("configGetUsers2")
    public ActionResult<?> configGetUsers2() {
        List<UserModel> userModels = userMapper.configGetUsers2();
        return success(userModels);
    }
    @ApiOperation("添加用户")
    @PostMapping("addUser")
    public ActionResult<?> addUser(@RequestBody @Valid AddUserModel model) {

        model.setUserId(SnowFlake.nextId());

        userAnnotationMapper.addUser(model);
        return success(model);
    }

    @ApiOperation("删除用户")
    @PostMapping("deleteUser")
    public ActionResult<?> deleteUser(Long id) {
        System.out.println("用户Id:" + id);
        userAnnotationMapper.deleteUser(id);
        return success();
    }

    @ApiOperation("更新用户")
    @PostMapping("updateUser")
    public ActionResult<?> updateUser(@RequestBody AddUserModel model) {
        userAnnotationMapper.updateUser(model);
        return success();
    }

    @ApiOperation("查询用户")
    @PostMapping("getUser/{id}")
    public ActionResult<UserModel> getUser(@PathVariable("id") Long id) {
        UserModel user = userAnnotationMapper.getUser(id);
        return success(user);
    }

    @ApiOperation("查询用户")
    @PostMapping("getUsers")
    public ActionResult<List<UserEntity>> getUsers() {
        List<UserEntity> users = userAnnotationMapper.getUsers();
        return success(users);
    }

    @ApiOperation("添加角色")
    @PostMapping("addRole")
    public ActionResult<RoleEntity> addRole(@RequestBody @Valid AddRoleModel model) {

        RoleEntity role = new RoleEntity();
        role.setRoleId(SnowFlake.nextId());
        role.setCreateTime(new Date());
        role.setName(model.getName());

        roleMapper.addRole(role);
        return success(role);
    }

    @GetMapping("getRole/{roleId}")
    public ActionResult<?> getRole(@PathVariable("roleId") Long roleId) {
        RoleEntity role = roleMapper.getRole(roleId);
        return success(role);

    }

    @GetMapping("getRoles")
    public ActionResult<?> getRoles() {
        List<RoleEntity> roles = roleMapper.getRoles();
        return success(roles);

    }

    @PostMapping("addUserRole")
    public ActionResult<?> addUserRole(@Valid AddUserRoleModel model) {
        UserRoleEntity entity = new UserRoleEntity();
        entity.setId(SnowFlake.nextId());
        entity.setUserId(model.getUserId());
        entity.setRoleId(model.getRoleId());
        userRoleMapper.addUserRole(entity);
        return success(entity);
    }

    @GetMapping("getUserRole")
    public ActionResult<?> getUserRole()
    {
        List<UserModel> userRole = userMapper.getUserRole();
        System.out.println(userRole);
        return success(userRole);
    }

    @GetMapping("testParams")
    public  ActionResult<?> testParams(@RequestParam(value = "id",required = true,defaultValue = "0") String id)
    {
        return success(id);
    }
}
